package cn.xiaomubiao.oschine.adapter.myinfo;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cn.xiaomubiao.oschine.R;
import cn.xiaomubiao.oschine.util.UIUtils;

import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;

/**
 * @author
 * @version 1.0
 * @date 2017/4/5
 */

public abstract class MyInfoBaseListAdapter<T> extends RecyclerView.Adapter{
    protected List<T> datas = new ArrayList<>();
    
    public OnItemClickListener listener;
    
    
    
    public  interface  OnItemClickListener{
        void onItemClickListener(View view, int position);
    }
    
    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }
    
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        
        MyViewHolder holder;
        if (viewType ==0){
            View view = LayoutInflater.from(UIUtils.getContext()).inflate(getLayoutId(), parent, false);
            holder = new MyViewHolder(view);
        }else {
            View view = LayoutInflater.from(UIUtils.getContext()).inflate(R.layout.view_item_myinfo_final, parent, false);
            holder = new MyViewHolder(view);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (listener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClickListener(v,position);
                }
            });
        }
        Log.d(TAG, "onBindViewHolder: ");
//        int itemViewType = getItemViewType(position);
//        if (itemViewType == 0){
//          
//        }else {
//            Log.d(TAG, "onBindViewHolder: ");
//        }
        initItemView(holder.itemView ,position);
        
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

//    @Override
//    public int getItemViewType(int position) {
////        if (position == datas.size()){
////            return 1;
////        }else {
////            return 0;
////        }
//    }

    protected abstract void initItemView(View itemView, int position);

    public abstract int getLayoutId();
    
    public void addDatas(List<T> datas){
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }
    

    
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public MyViewHolder(View itemView) {
            super(itemView);
        }
    }

    


}
